<!-- 接口列表 -->

<template>
  <div class="pgTitle">账号管理列表</div>
  <div class="pgCont">
    <div class="listWrap">
      <div class="listItem" v-for="(item, index) in reactiveData.tableData" :key="index">
        <el-form-item label="请输入账号" class="">
          <el-input v-model="item.username" placeholder="请输入" clearable/>
        </el-form-item>
        <el-form-item label="请输入密码" class="">
          <el-input v-model="item.password" placeholder="请输入" clearable/>
        </el-form-item>
        <el-form-item label="登录验证方式" class="password">
<!--          <el-input v-model="item.target" placeholder="请输入" clearable/>-->
          <el-select
            v-model="item.visibleType"
            clearable
            placeholder="请选择"
            @change="visibleTypeChange"
          >
            <el-option
              v-for="item in visibleTypeDict"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            />
          </el-select>
        </el-form-item>

        <el-form-item v-if="item.visibleType === '1'" label="输入cookie的Key" class="">
          <el-input v-model="item.cookieKey" placeholder="请输入" clearable/>
        </el-form-item>
        <el-form-item v-if="item.visibleType === '1'" label="输入cookie的Value" class="">
          <el-input v-model="item.cookieName" placeholder="请输入" clearable/>
        </el-form-item>

        <el-form-item v-if="item.visibleType === '2'" label="输入token的Key" class="">
          <el-input v-model="item.tokenKey" placeholder="请输入" clearable/>
        </el-form-item>
        <el-form-item v-if="item.visibleType === '2'" label="输入token的Value" class="">
          <el-input v-model="item.tokenName" placeholder="请输入" clearable/>
        </el-form-item>

        <el-form-item label="登录返回数据父级" class="">
          <el-input v-model="item.target" placeholder="请输入" clearable/>
        </el-form-item>
        <el-form-item label="备注描述" class="">
          <el-input v-model="item.remark" placeholder="请输入" clearable/>
        </el-form-item>
        <el-form-item label="补充个人信息字段" class="longInput">
          <el-input
            v-model="item.moreInfoParams"
            placeholder="请输入"
            clearable
            type="textarea"
            rows="1"
          />
        </el-form-item>
        <el-form-item label="" class="longInput">
          <el-button @click="reactiveData.login(item)" class="removeBtn">测试登录</el-button>
          <el-button @click="reactiveData.removeItem(index)" class="removeBtn">删除</el-button>
        </el-form-item>
      </div>
      <div class="addItem">
        <el-button @click="reactiveData.addItem()">+ 添加账户</el-button>
        <el-button type="primary" @click="reactiveData.saveItem">保存</el-button>
      </div>
    </div>

  </div>
</template>

<script setup lang="ts">
import {reactive, ref} from 'vue'
import axios from "@/utils/axios";
import { ElMessage } from 'element-plus'


const visibleTypeDict = [
  {name:'请求头写入cookie', type:'1'},
  {name:'接口返回token', type:'2'},
]

const visibleTypeChange = (value)=>{
  console.log('1231231', value)
}
// table
const reactiveData = reactive({
  tableData: <any>[
    {username:'', password:'', target:'', remark:'', visibleType:'', cookieKey:'', cookieName:'', tokenKey:'', tokenName:'', moreInfoParams:''},
  ],
  addItem() {
    const item = JSON.parse(JSON.stringify({username:'', password:'', target:'', remark:'', visibleType:'', cookieKey:'', cookieName:'', tokenKey:'', tokenName:'', moreInfoParams:'' }))
    reactiveData.tableData.push(item)
  },
  removeItem( index:string|number ){
    reactiveData.tableData.splice(index, 1)
  },
  saveItem( index:string|number ){
    localStorage.setItem('AccountList',JSON.stringify(reactiveData.tableData))
    setTimeout(()=>{
      ElMessage.success("保存成功")
    },333)
  },
  pageTotal: 0,
  inputChange(row:any){
    row.disabled = true
  },
  inputSave(row:any){
    // axios.post 保存参数更改，保存成功后提示
    row.disabled = false

    setTimeout(()=>{
      ElMessage.success("保存成功")
    },333)
  },

  login(item:any) {
    // console.log('item', item)
    // axios.get('/login', { params:
    //     {username: item.username, passwd: item.password}
    // })
    //   .then((res: any) => {
    //     console.log('res>>>', res)
    //   })
    axios.post('/login', {username: item.username, passwd: item.password})
      .then((res: any) => {
        console.log('res>>>', res)
      })
  }
})

// 读取缓存
const AccountList = localStorage.getItem('AccountList')
if( AccountList ) reactiveData.tableData = JSON.parse(AccountList)

</script>

<style scoped lang="stylus" >
.removeBtn{
  display: none;
}
.listItem{
  padding-left: 10px;
  &:hover{
    background-color: #f2f2f2;
    .removeBtn{
      display: block;
    }
  }
}

.addItem{
  margin-left: 10px;
  padding-top: 20px;
}


:deep(.listItem){
  .el-form-item{
    margin-right: 10px;
    display: inline-block;
    width: 150px;
  }
  .longInput{
    width:200px
  }
}
.xxx {
  display: flex;
  .input{
    flex: 1; padding-right: 10px;
  }
  .button{
  }
}
</style>
